package com.smec.remes.module.register;

import android.app.ProgressDialog;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.smec.moblie.R;
import com.smec.moblie.SMECApplication;
import com.smec.moblie.databinding.ActivityRegisterBinding;
import com.smec.remes.base.SmecBaseFragment;
import com.smec.remes.bean.AbleMatnr;
import com.smec.remes.bean.SearchContract;
import com.smec.remes.bean.SubmitRegister;
import com.smec.remes.bean.SubmitRegisterChildren;
import com.smec.remes.module.register.activity.SmecContractActivity;
import com.smec.remes.module.register.activity.SmecMantrActivity;
import com.smec.remes.module.register.adapter.SmecRegisterAdapter;
import com.smec.remes.smecManager.SmecRetrofit.HttpResult;
import com.smec.remes.util.CommonUtils;
import com.smec.remes.util.RemarkDialog;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/12/7.
 */

public class SmecRegisterFragment extends SmecBaseFragment<SmecRegisterPresenter> {

    private ActivityRegisterBinding activityRegisterBinding;
    public static final String contractCheck="contractCheck";
    public static final String contractCheckFAIL="contractCheckFAIL";
    public static final String getCompletedNum="getCompletedNum";
    public static final String Contract="Contract";
    public static final String ContractEdit="ContractEdit";
    public static final String BuyList="BuyList";
    private ArrayList<AbleMatnr> arrayList=new ArrayList<>();
    private SmecRegisterAdapter registerAdapter;
    private ProgressDialog progressDialog;
    public static final String SUBMIT_SUCCESS="SUBMIT_SUCCESS";
    public static final String SUBMIT_FAILED="SUBMIT_FAILED";
    public static final String SUBMIT_FAILED_M="SUBMIT_FAILED_M";
    private String eleContractNo;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        activityRegisterBinding =DataBindingUtil.inflate(inflater,R.layout.activity_register,container,false);
        registerAdapter=new SmecRegisterAdapter(getActivity());
        activityRegisterBinding.regitsterRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        activityRegisterBinding.regitsterRecyclerView.setAdapter(registerAdapter);
        _mantr();
        _contract();
        _setVis();
        _submit();
        _getMsg();
        mPresenter.prompt();
//        mPresenter._Month("","MONTH",1);
        return activityRegisterBinding.getRoot();
    }

    public void _getMsg(){
        activityRegisterBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                if(!TextUtils.isEmpty(activityRegisterBinding.edRequestnum.getText().toString())){
                    activityRegisterBinding.llBtn.setVisibility(View.GONE);
                    activityRegisterBinding.llEd.setVisibility(View.VISIBLE);
                    activityRegisterBinding.llTv.setVisibility(View.VISIBLE);
                }
            }
        },500);
    }

    public void _submit(){
        activityRegisterBinding.btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!TextUtils.isEmpty(activityRegisterBinding.edRequestnum.getText().toString())){
                    mPresenter.contractCheck(activityRegisterBinding.edRequestnum.getText().toString());
                }else {
                    Toast.makeText(getActivity(),"请完善物料信息",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void _mantr(){
        activityRegisterBinding.btnMantr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!TextUtils.isEmpty(activityRegisterBinding.edRequestnum.getText())){
                    Intent intent=new Intent(getContext(), SmecMantrActivity.class);
                    startActivity(intent);
                    activityRegisterBinding.btnMantr.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if(CommonUtils.notEmpty(registerAdapter._getList())){
                                mRxBus.post("SelectMantr",arrayList);
                            }
                        }
                    },500);
                }else {
                    Toast.makeText(getActivity(),"请先选择或填完产品合同号梯号",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void _contract(){
        activityRegisterBinding.btnProduct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(), SmecContractActivity.class);
                startActivity(intent);

            }
        });
    }

    public void _setVis(){
        activityRegisterBinding.llRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(activityRegisterBinding.edRequestnum.getText().toString().equals("")){
                    activityRegisterBinding.llEd.setVisibility(View.GONE);
                    activityRegisterBinding.llTv.setVisibility(View.GONE);
                    activityRegisterBinding.llBtn.setVisibility(View.VISIBLE);
                    CommonUtils.hideImmManager(activityRegisterBinding.edRequestnum);
                }
            }
        });

        activityRegisterBinding.ivNameDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activityRegisterBinding.llEd.setVisibility(View.GONE);
                activityRegisterBinding.llTv.setVisibility(View.GONE);
                activityRegisterBinding.llBtn.setVisibility(View.VISIBLE);
                CommonUtils.hideImmManager(activityRegisterBinding.edRequestnum);
            }
        });
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(getCompletedNum)
            }
    )
    public void _getCompletedNum(ArrayList<String> arrayList){
        if(CommonUtils.notEmpty(arrayList)){
            activityRegisterBinding.prompt.setVisibility(View.VISIBLE);
            activityRegisterBinding.prompt.setText(arrayList.get(0)+"个单子已完结，"+arrayList.get(1)+"种物料未绑定");
        }
    }


    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(Contract)
            }
    )

    public void _getContract(SearchContract searchContract){
        activityRegisterBinding.setContract(searchContract);
        eleContractNo=searchContract.getEleContractNo();
        activityRegisterBinding.llBackground.setVisibility(View.VISIBLE);
        activityRegisterBinding.llBtn.setVisibility(View.GONE);
        activityRegisterBinding.llEd.setVisibility(View.VISIBLE);
        activityRegisterBinding.llTwo.setVisibility(View.VISIBLE);
        activityRegisterBinding.llThree.setVisibility(View.VISIBLE);
        activityRegisterBinding.llFour.setVisibility(View.VISIBLE);
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(contractCheck)
            }
    )

    public void _contractCheck(HttpResult httpResult){
        String str=httpResult.getCode();
        if(str.equals("SUCCESS")){
            subMsg(null);
        }
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(contractCheckFAIL)
            }
    )

    public void _contractCheckFAIL(String a){
        RemarkDialog remarkDialog=new RemarkDialog(getContext(), R.style.project_back, new RemarkDialog.Dialoglistener() {
            @Override
            public void cancle(RemarkDialog dialog) {
                dialog.dismiss();
            }

            @Override
            public void makeSure(String remark,RemarkDialog dialog) {
                if (remark == null || remark.equals("")){
                    Toast.makeText(getContext(),"请输入原因",Toast.LENGTH_SHORT).show();
                    return;
                }
                subMsg(remark);
                dialog.dismiss();
            }
        });
        remarkDialog.show();
    }

    private void subMsg(String remark){
        SubmitRegister submitRegister=new SubmitRegister();
        submitRegister.setRemark(remark);
        if(!TextUtils.isEmpty(activityRegisterBinding.edRequestnum.getText().toString())&& CommonUtils.notEmpty(arrayList)){
            submitRegister.setEleContractNo(activityRegisterBinding.edRequestnum.getText().toString());
            ArrayList<SubmitRegisterChildren> submitRegisterChildrenArrayList=new ArrayList<>();
            for(int i=0;i<arrayList.size();i++){
                SubmitRegisterChildren submitRegisterChildren=new SubmitRegisterChildren();
                submitRegisterChildren.setMatnr(arrayList.get(i).getMatnr());
                submitRegisterChildren.setMatnrName(arrayList.get(i).getMatnrName());
                submitRegisterChildren.setUseQty(Integer.parseInt(arrayList.get(i).getNum()));
                submitRegisterChildren.setPkgUnit(arrayList.get(i).getPkgUnit());
                submitRegisterChildren.setMntStation(arrayList.get(i).getMntStation());
                submitRegisterChildren.setMntCustomerName(arrayList.get(i).getCustomerName());
                submitRegisterChildrenArrayList.add(submitRegisterChildren);
            }
            submitRegister.setMatnrList(submitRegisterChildrenArrayList);
            if(progressDialog == null){
                progressDialog=new ProgressDialog(getContext());
                progressDialog.show();
            }else {
                if(!progressDialog.isShowing()){
                    progressDialog.show();
                }
            }
            mPresenter.submitRegister(submitRegister);
        }else {
            Toast.makeText(getActivity(),"请完善物料信息",Toast.LENGTH_SHORT).show();
        }
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(ContractEdit)
            }
    )
    public void _edit(String a){
        activityRegisterBinding.llBackground.setVisibility(View.GONE);
        activityRegisterBinding.llFour.setVisibility(View.GONE);
        activityRegisterBinding.llTv.setVisibility(View.VISIBLE);
        activityRegisterBinding.llBtn.setVisibility(View.GONE);
        activityRegisterBinding.llEd.setVisibility(View.VISIBLE);
        activityRegisterBinding.llTwo.setVisibility(View.GONE);
        activityRegisterBinding.llThree.setVisibility(View.GONE);
        activityRegisterBinding.edRequestnum.setText("");
        activityRegisterBinding.edRequestnum.requestFocus();
        activityRegisterBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                CommonUtils.showImmManager(activityRegisterBinding.edRequestnum);
            }
        },300);
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(BuyList)
            }
    )

    public void _setList(ArrayList<AbleMatnr> getBuycarList){
        if(CommonUtils.notEmpty(getBuycarList)){
            arrayList=getBuycarList;
            registerAdapter.setList(arrayList);
        }else {
            registerAdapter.setArrayList(new ArrayList<AbleMatnr>());
        }
    }

    public static SmecRegisterFragment newInstance() {
        Bundle args = new Bundle();
        SmecRegisterFragment fragment = new SmecRegisterFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(SUBMIT_FAILED)
            }
    )
    public void submitFaild(Object o){

        activityRegisterBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                if(progressDialog!=null){
                    progressDialog.dismiss();
                }
            }
        },700);
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(SUBMIT_FAILED_M)
            }
    )
    public void fail(String o){

        Toast.makeText(SMECApplication.getContext(),o,Toast.LENGTH_SHORT).show();;

        activityRegisterBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                if(progressDialog!=null){
                    progressDialog.dismiss();
                }
            }
        },700);
    }


    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(SUBMIT_SUCCESS)
            }
    )
    public void submitSuccess(Object o){
        activityRegisterBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                if(progressDialog!=null){
                    progressDialog.dismiss();
                }
            }
        },700);
        activityRegisterBinding.edRequestnum.setText("");
        registerAdapter.clearList();
        activityRegisterBinding.llTv.setVisibility(View.GONE);
        activityRegisterBinding.llEd.setVisibility(View.GONE);
        activityRegisterBinding.llBtn.setVisibility(View.VISIBLE);
        Toast.makeText(getActivity(),"物料信息登记成功",Toast.LENGTH_SHORT).show();

    }

    @Override
    protected SmecRegisterPresenter getPersenter() {
        return new SmecRegisterPresenter(getActivity());
    }
}
